package com.edu.zju.scs.panacea.mapper.resource;

import com.edu.zju.scs.panacea.entity.resource.Dev;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-13 19:11
 */

@Repository
public interface DevMapper {

    @Select("SELECT * FROM res_dev WHERE id = #{id}")
    @Results(id="devMap", value={
            @Result(id=true,property="id",column="id"),
            @Result(property="ownId",column="own_id"),
            @Result(property="name",column="name"),
            @Result(property="probeId",column="probe_id"),
            @Result(property="siteId",column="site_id"),
            @Result(property="height",column="height"),
            @Result(property="width",column="width"),
            @Result(property="longitude",column="longitude"),
            @Result(property="latitude",column="latitude"),
            @Result(property="type",column="type")})

    Dev selectDev(Long id);


    @Select("SELECT * FROM res_dev WHERE site_id = #{siteId}")
    @ResultMap(value = "devMap")
    List<Dev> selectDevBySiteId(Long siteId);

    @Select("SELECT * FROM res_dev WHERE probe_id = #{probeId}")
    @ResultMap(value = "devMap")
    Dev selectDevByProbe(String probeId);

    @Select("SELECT * FROM res_dev WHERE site_id = #{siteId} and res_dev.id in (select dev_id from res_loc)")
    @ResultMap(value = "devMap")
    List<Dev> selectResDevBySiteId(Long siteId);

    @Select("SELECT * FROM res_dev WHERE site_id = #{siteId} and res_dev.id not in (select dev_id from res_loc)")
    @ResultMap(value = "devMap")
    List<Dev> selectNotResDevBySiteId(Long siteId);


    @Select("SELECT * FROM res_dev")
    @ResultMap(value = "devMap")
    List<Dev> selectAllDev();

    @Delete("DELETE FROM res_dev where id = #{id}")
    long deleteDev(Long id);

    @Insert("insert into res_dev (own_id, name, probe_id, site_id, height, width, longitude, latitude, type) values" +
            "(#{ownId}, #{name}, #{probeId}, #{siteId}, #{height}, #{width}, #{longitude}, #{latitude}, #{type})")
    @Options(useGeneratedKeys=true, keyColumn="id")
    long insertDev(Dev dev);

    @Update({ "update res_dev set own_id = #{ownId}, name = #{name}, probe_id = #{probeId}, site_id = #{siteId}, height = #{height}," +
            "width = #{width}, longitude = #{longitude}, latitude = #{latitude}, type = #{type} where id = #{id}" })
    long updateDev(Dev dev);


}
